作者:阿贵 | 来源:互联网 | 2023-09-23 20:43
一、IngressNginx概述###---IngressNginx重定向:域名重定向到另外一个域名~~~#实例一:http转成https会使用到~~~#实例二:新旧域名的替换会使
一、IngressNginx概述
### --- IngressNginx重定向:域名重定向到另外一个域名
~~~ # 实例一:http转成https会使用到
~~~ # 实例二:新旧域名的替换会使用到
二、创建IngressNginx容器
### --- 创建nginx.deployment.yaml文件
~~~ 创建nginx-deployment
[root@k8s-master01 IngressNginx]# vim nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations: {}
labels:
app: ingress-test
name: ingress-test
namespace: ratel-test1
spec:
replicas: 1
selector:
matchLabels:
app: ingress-test
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: ingress-test
spec:
affinity: {}
containers:
- env:
- name: TZ
value: Asia/Shanghai
- name: LANG
value: C.UTF-8
image: nginx
imagePullPolicy: IfNotPresent
lifecycle: {}
name: ingress-test
ports:
- containerPort: 80
name: web
protocol: TCP
resources:
limits:
cpu: 100m
memory: 100Mi
requests:
cpu: 10m
memory: 10Mi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /usr/share/zoneinfo/Asia/Shanghai
name: tz-config
- mountPath: /etc/localtime
name: tz-config
- mountPath: /etc/timezone
name: timezone
dnsPolicy: ClusterFirst
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes:
- hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai
type: ""
name: tz-config
- hostPath:
path: /etc/timezone
type: ""
name: timezone
三、创建nginx-deployment
### --- 创建nginx-deployment
[root@k8s-master01 IngressNginx]# kubectl create -f nginx-deployment.yaml
### --- 查看创建的po、svc、ep
[root@k8s-master01 IngressNginx]# kubectl get po,svc,ep -n ratel-test1 -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/ingress-test-6dd476fb56-twrdf 1/1 Running 0 8m24s 172.25.244.203 k8s-master01
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/ingress-test ClusterIP 10.106.126.20 80/TCP 8m24s app=ingress-test
NAME ENDPOINTS AGE
endpoints/ingress-test 172.25.244.203:80 8m24s
### --- 验证通过service访问nginx
[root@k8s-master01 IngressNginx]# curl 10.106.126.20
Welcome to nginx!
四、创建一个nginx-ingress
### --- 创建nginx-ingress-demo.yaml文件
[root@k8s-master01 IngressNginx]# vim nginx-ingress-demo.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: ingress-test
namespace: ratel-test1
spec:
rules:
- host: ingress.test.com
http:
paths:
- backend:
serviceName: ingress-test
servicePort: 80
path: /
### --- 创建nginx-ingress-demo
[root@k8s-master01 IngressNginx]# kubectl create -f nginx-ingress-demo.yaml
ingress.extensions/ingress-test created
### --- 配置本地hosts文件
~~~ 配置hosts,把域名直接指到ingress服务器上,
~~~ 但是生产环境中,是不建议这样使用的,因为生产服务器是不会直接暴露在公网上面的
~~~ 一般都是域名厂商把域名解析到外部的SLB或者外部的负载均衡上,然后负载均衡再到服务上
192.168.1.11 ingress.test.com
五、通过域名访问nginx.pod :地址:http://ingress.test.com/
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor